package com.jiangjiang.eshop.promotion.biz;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiangjiang.eshop.promotion.constant.PromotionActivityStatus;
import com.jiangjiang.eshop.promotion.convert.PromotionActivityConvert;
import com.jiangjiang.eshop.promotion.domain.PromotionActivityEntity;
import com.jiangjiang.eshop.promotion.domain.PromotionActivityGoodsRelationEntity;
import com.jiangjiang.eshop.promotion.dto.PromotionActivityDTO;
import com.jiangjiang.eshop.promotion.dto.PromotionActivityQueryPageDTO;
import com.jiangjiang.eshop.promotion.service.PromotionActivityGoodsRelationService;
import com.jiangjiang.eshop.promotion.service.PromotionActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName PromotionActivityBiz
 * @Description TODO
 * @Author jhw
 * @Date 2021/7/15 11:07
 * @Version 1.0
 **/
@Component
public class PromotionActivityBiz {
    @Autowired
    private PromotionActivityService promotionActivityService;
    @Autowired
    private PromotionActivityConvert promotionActivityConvert;
    @Autowired
    private PromotionActivityGoodsRelationService promotionActivityGoodsRelationService;

    /**
     * 新增促销活动
     *
     * @param dto 促销活动
     */
    public void save(PromotionActivityDTO dto) {
        // 保存营销活动数据
        PromotionActivityEntity entity = promotionActivityConvert.convertDTO2Entity(dto);
        // 默认停用状态
        entity.setStatus(PromotionActivityStatus.DISABLED);
        promotionActivityService.save(entity);
        // 保存营销活动和商品之间的关联关系
        promotionActivityGoodsRelationService.saveBatch(dto.getRelations());
    }

    /**
     * 营销活动更新
     *
     * @param dto 促销活动
     */
    public void updatePromotion(PromotionActivityDTO dto) {
        PromotionActivityEntity entity = promotionActivityConvert.convertDTO2Entity(dto);
        promotionActivityService.updateById(entity);

        // 清空营销活动关联与商品的关联关系
        promotionActivityGoodsRelationService.removeByPromotionId(dto.getId());
        // 保存新的关联关系
        promotionActivityGoodsRelationService.saveBatch(dto.getRelations());

    }


    /**
     * 分页查询营销活动数据
     *
     * @param dto 营销活动分页查询条件DTO
     * @return 分页数据
     */
    public Page<PromotionActivityDTO> queryForPage(PromotionActivityQueryPageDTO dto) {
        return promotionActivityConvert.convertPage(promotionActivityService.queryForPage(dto));
    }


    /**
     * 根据商品id 查询促销活动
     *
     * @param goodsId 商品id
     * @return 促销活动
     */
    public List<PromotionActivityDTO> listByGoodsId(Long goodsId) {

        // 查询营销活动关联的商品数据
        Map<Long, List<PromotionActivityGoodsRelationEntity>> relationMap = promotionActivityGoodsRelationService.listByGoodsId(goodsId)
                .stream()
                .collect(Collectors.groupingBy(PromotionActivityGoodsRelationEntity::getPromotionActivityId));

        return promotionActivityConvert.convertEntities2DTOList(promotionActivityService.listByIds(relationMap.keySet()))
                .stream()
                .peek(dto -> {
                    // 营销活动和商品的关联关系
                    dto.setRelations(relationMap.get(dto.getId()));
                }).collect(Collectors.toList());


    }

    /**
     * 根据id查询优惠活动
     *
     * @param id 营销活动id
     * @return 营销活动DTO
     */
    public PromotionActivityDTO queryById(Long id) {
        PromotionActivityDTO dto = promotionActivityConvert.convertEntity2DTO(promotionActivityService.getById(id));
        List<PromotionActivityGoodsRelationEntity> relations = promotionActivityGoodsRelationService.queryByPromotionActivityId(id);
        dto.setRelations(relations);
        return dto;
    }

    /**
     * 删除促销活动
     * @param promotionActivityId 促销活动id
     */
    public void removeByActivityId(Long promotionActivityId) throws Exception {
        promotionActivityService.removeById(promotionActivityId);
        promotionActivityGoodsRelationService.removeByPromotionId(promotionActivityId);
    }



}
